#include "RegionWindow.hpp"

RegionWindow::RegionWindow(){
    setSize(150, 80);
    setTitle("Region properties");
    
    lblName.setString(10, sf::Color::White, sf::Text::Regular, "Name:");
    lblName.setPosition(5, 0);
    contentPanel.add(&lblName);
    txtName.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    txtName.setPosition(lblName.rect.left + lblName.labelText.getLocalBounds().width + 4, 0);
    txtName.maxLength = REGION_NAME_LENGTH;
    txtName.setSize(100, 12);
    contentPanel.add(&txtName);

    lblScript.setString(10, sf::Color::White, sf::Text::Regular, "Script:");
    lblScript.setPosition(5, 15);
    contentPanel.add(&lblScript);
    txtScript.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    txtScript.setPosition(lblScript.rect.left + lblScript.labelText.getLocalBounds().width + 4, 15);
    txtScript.maxLength = OBJECT_SCRIPT_LENGTH;
    txtScript.setSize(100, 12);
    contentPanel.add(&txtScript);

    lblX.setString(10, sf::Color::White, sf::Text::Regular, "X pos:");
    lblX.setPosition(5, 30);
    contentPanel.add(&lblX);
    txtX.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    txtX.setPosition(lblX.rect.left + lblX.labelText.getLocalBounds().width + 4, 30);
    txtX.maxLength = 6;
    txtX.setSize(50, 12);
    contentPanel.add(&txtX);
    
    lblY.setString(10, sf::Color::White, sf::Text::Regular, "Y pos:");
    lblY.setPosition(75, 30);
    contentPanel.add(&lblY);
    txtY.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    txtY.setPosition(lblY.rect.left + lblY.labelText.getLocalBounds().width + 4, 30);
    txtY.maxLength = 6;
    txtY.setSize(50, 12);
    contentPanel.add(&txtY);
    
    lblWidth.setString(10, sf::Color::White, sf::Text::Regular, "Width:");
    lblWidth.setPosition(5, 45);
    contentPanel.add(&lblWidth);
    txtWidth.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    txtWidth.setPosition(lblWidth.rect.left + lblWidth.labelText.getLocalBounds().width + 4, 45);
    txtWidth.maxLength = 6;
    txtWidth.setSize(50, 12);
    contentPanel.add(&txtWidth);

    lblHeight.setString(10, sf::Color::White, sf::Text::Regular, "Height:");
    lblHeight.setPosition(75, 45);
    contentPanel.add(&lblHeight);
    txtHeight.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    txtHeight.setPosition(lblHeight.rect.left + lblHeight.labelText.getLocalBounds().width + 4, 45);
    txtHeight.maxLength = 6;
    txtHeight.setSize(50, 12);
    contentPanel.add(&txtHeight);
    trap(InterfaceEvent::ReleasedKeyboard | InterfaceEvent::LostKeyboard
        | InterfaceEvent::GotKeyboard);
    visible = false;
}

/**
* Set the region the window is focused on.
*/
void RegionWindow::setRegion(EditorRegion* region){
    selectedRegion = region;
    
    if(selectedRegion){
        visible = true;
        txtName.setString(selectedRegion->name);
		txtScript.setString(selectedRegion->script);
        txtHeight.setString(StringHelper::itos(selectedRegion->height));
        txtWidth.setString(StringHelper::itos(selectedRegion->width));
        txtX.setString(StringHelper::itos(selectedRegion->x));
        txtY.setString(StringHelper::itos(selectedRegion->y));
    }else{
        visible = false;
    }
}

/**
 * Raise a custom event that signifies the Region related InterfaceElements should be updated.
 */
bool RegionWindow::trapped(InterfaceEvent* event){
    if(event->type == InterfaceEvent::ReleasedKeyboard || event->type == InterfaceEvent::LostKeyboard){
        strncpy(selectedRegion->name, txtName.text.c_str(), REGION_NAME_LENGTH);
        selectedRegion->setArea(StringHelper::stoi(txtX.text.c_str()), StringHelper::stoi(txtY.text.c_str()),
                StringHelper::stoi(txtWidth.text.c_str()), StringHelper::stoi(txtHeight.text.c_str()));
		selectedRegion->setScript(txtScript.getString());
        Window::raise(InterfaceEvent::Custom);
        return true;
    }else if(event->type == InterfaceEvent::GotKeyboard){
        Window::raise(InterfaceEvent::Custom);
        return true;
    }
    
    return Window::trapped(event);
}